package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Permission;
import cn.wolfcode.rbac.qo.PageQo;
import cn.wolfcode.rbac.qo.QueryObject;
import cn.wolfcode.rbac.service.IPermissionService;
import cn.wolfcode.rbac.web.annotation.RequiredPermission;
import cn.wolfcode.rbac.utils.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@CrossOrigin(allowCredentials = "true")
@RequestMapping("/api/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequiredPermission(name = "权限加载",expression = "permission:load")
//    @PreAuthorize("hasAuthority('permission:load')")
    @PostMapping("load")
    public R load(){
        permissionService.load();
        return R.ok("权限加载成功");
    }

    @RequiredPermission(name = "权限列表",expression = "permission:list")
//    @PreAuthorize("hasAuthority('permission:list')")
    @GetMapping("list")
    public R list(QueryObject qo){
        PageInfo<Permission> pageInfo= permissionService.list(qo);
        PageQo pageQo = new PageQo(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getList(), pageInfo.getTotal());
        return R.ok(pageQo);
    }

    @RequiredPermission(name = "根据角色id查询权限信息",expression = "permission:queryPermission")
//    @PreAuthorize("hasAuthority('permission:queryPermission')")
    @GetMapping("queryPermission/{id}")
    public R queryPermission(@PathVariable Long id){
        List<Permission> permissionList = permissionService.queryPermission(id);
        return R.ok(permissionList);
    }
}
